package com.jsb.laundry.controller;

import com.jsb.laundry.common.constant.CommonConstants;
import com.jsb.laundry.common.response.WebResponse;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.dao.ProductDao;
import com.jsb.laundry.model.dto.GoodsInfoRequest;
import com.jsb.laundry.model.entity.ActivityModuleDetail;
import com.jsb.laundry.model.entity.LaundryActivityBnner;
import com.jsb.laundry.model.vo.LaundryCategoryVo;
import com.jsb.laundry.model.vo.LaundryPriceVo;
import com.jsb.laundry.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"商品服务"})
@RestController
@RequestMapping("/userProduct")
public class ProductController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);


    @Resource
    private ProductDao productDao;
    @Resource
    private ProductService productService;

    /**
     * 查询出所有分类及分类下商品
     */
    @ApiOperation(value = "获取所有分类及分类下商品", notes = "查询所有分类及分类下商品", httpMethod = "POST")
    @PostMapping("/findAllCateAndGoods")
    public WebResponse<List<LaundryCategoryVo>> findAllCateAndGoods() {
        /**
         * 对特殊商品在确认订单页面的特殊提示
         */
        //指定的几种固定的serveId，在确认订单页面要有特殊提示
        List<String> databaseServeList = Arrays.asList("223", "224", "225", "226", "227", "228");

        List<LaundryCategoryVo> goodsDataList = productDao.findAllCateAndGoods();
        for (LaundryCategoryVo cate : goodsDataList) {

            //对特殊商品在确认订单页面的特殊提示
            for (LaundryCategoryVo.LaundryCategoryChildren laundryCategoryChildren : cate.getGoodsDataList()) {
                if (databaseServeList.contains(laundryCategoryChildren.getServeId())) { //获取本次serveId与特殊服务对比
                    laundryCategoryChildren.setHintFlag("1");
                } else {
                    laundryCategoryChildren.setHintFlag("0");
                }
            }

        }
        return WebResponse.successData(goodsDataList);
    }


    /**
     * 衣服的不同洗护类型价格
     *
     * @param goodsInfo
     * @return
     */
    @ApiOperation(value = "衣服的不同洗护类型价格", notes = "衣服的不同洗护类型价格", httpMethod = "POST")
    @PostMapping("/findServeByGoodsId")
    public WebResponse<List<LaundryPriceVo>> findServeByGoodsId(@RequestBody GoodsInfoRequest goodsInfo) {
        LOGGER.info("衣服的不同洗护类型价格" + goodsInfo);
        if (JSBUtil.isEmptyString(goodsInfo.getGoodsId())) {
            return WebResponse.failOfParameter();
        }
        List<LaundryPriceVo> serveIdList = productService.findServeByGoodsId(goodsInfo.getGoodsId());
        return WebResponse.successData(serveIdList);
    }

    /**
     * 按衣物名模糊搜索(goodsTitle)
     */
    @ApiOperation(value = "按衣物名模糊搜索", notes = "按衣物名模糊搜索", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsTitle", value = "商品标题", required = true, paramType = "body", dataType = "String")
    })
    @PostMapping("/searchByClothesTitle")
    public WebResponse searchByClothesTitle(@RequestBody GoodsInfoRequest goodsInfo) {
        LOGGER.info("按衣物名模糊搜索:" + goodsInfo.getGoodsTitle());
        //goodsTitle 请求参数判空
        if (JSBUtil.isEmptyString(goodsInfo.getGoodsTitle())) {
            return WebResponse.failOfParameter();
        }
        List<LaundryCategoryVo.LaundryCategoryChildren> returnMsg = productDao.searchByClothesTitle(goodsInfo.getGoodsTitle());

        return WebResponse.successData(returnMsg);
    }

    /**
     * 获取活动详情
     */
    @PostMapping("/activityDetail")
    public WebResponse activityDetail(@RequestBody ActivityModuleDetail detail) {
        LOGGER.info("获取活动详情,活动ID:" + detail.getActivityId());
        if (JSBUtil.isEmptyString(detail.getActivityId())) {
            return WebResponse.failOfParameter();
        }
        ActivityModuleDetail schoolGoodsDetailV1 = productDao.activityDetail(detail.getActivityId());
        //详情图片是多张分开的，要统一放入detailPicList中
        String detailPic = schoolGoodsDetailV1.getDetailPic();
        List<String> strings = Arrays.asList(detailPic.split(CommonConstants.COMMA));
        schoolGoodsDetailV1.setDetailPicList(strings);
        //商品说明，要统一放入detailPicList中 数据库中存时|分割
        String goodsIntroduce = schoolGoodsDetailV1.getGoodsIntroduce();
        List<String> strings1 = Arrays.asList(goodsIntroduce.split(CommonConstants.VERTICAL_LINE));
        schoolGoodsDetailV1.setGoodsIntroduceList(strings1);

        //最低价set
        if (schoolGoodsDetailV1.getGoodsData().size() != 0) {
            schoolGoodsDetailV1.setGoodsPrice(schoolGoodsDetailV1.getGoodsData().get(0).getServePrice());
        }
        //服务 多个时,设置标识
        if (schoolGoodsDetailV1.getGoodsData().size() > 1) {
            schoolGoodsDetailV1.setFlag(1);
        }

        return WebResponse.successData(schoolGoodsDetailV1);
    }

    @ApiOperation(value = "活动轮播图", notes = "活动轮播图", httpMethod = "POST")
    @PostMapping("rotationChart")
    public WebResponse rotationChart(){
        List<LaundryActivityBnner> list = productDao.rotationChart();
        return WebResponse.successData(list);
    }
}
